package com.example.resturant.controller;

import com.example.resturant.dto.LoginRequestDTO;
import com.example.resturant.model.News;
import com.example.resturant.service.AdminService;
import com.example.resturant.vo.MenuVO;
import com.example.resturant.vo.ResultVO;
import lombok.var;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    @GetMapping("/login")
    public String login() {
        return "admin/login";
    }

    @GetMapping
    public String index(HttpSession httpSession) {
        return checkLogin(httpSession, "admin/menu");
    }

    @GetMapping("/order")
    public String order(HttpSession httpSession) {
        return checkLogin(httpSession, "admin/order");
    }

    @GetMapping("/menu")
    public String menu(HttpSession httpSession) {
        return checkLogin(httpSession, "admin/menu");
    }

    @GetMapping("/news")
    public String news(HttpSession httpSession) {
        return checkLogin(httpSession, "admin/news");
    }

    @GetMapping("/user")
    public String user(HttpSession httpSession) {
        return checkLogin(httpSession, "admin/user");
    }

    @GetMapping("/orders")
    @ResponseBody
    public ResultVO listOrders(@PageableDefault Pageable page) {
        var res = adminService.listOrders(page);
        return new ResultVO(){{setData(res);}};
    }

    @GetMapping("/users")
    @ResponseBody
    public ResultVO listUsers(@PageableDefault Pageable page) {
        var res = adminService.listUsers(page);
        return new ResultVO(){{setData(res);}};
    }

    @GetMapping("/menus")
    @ResponseBody
    public ResultVO listMenus(@PageableDefault(size = 5) Pageable page) {
        var res = adminService.listMenus(page);
        return new ResultVO(){{setData(res);}};
    }

    @GetMapping("/newsList")
    @ResponseBody
    public ResultVO newsList(@PageableDefault(size = 5) Pageable page) {
        var res = adminService.listNews(page);
        return new ResultVO(){{setData(res);}};
    }

    @PostMapping("/updateUserState")
    @ResponseBody
    public ResultVO updateUserState(@RequestParam Integer id) {
        adminService.updateUserState(id);
        return new ResultVO();
    }

    @PostMapping("/updateMenuState")
    @ResponseBody
    public ResultVO updateMenuState(@RequestParam Integer id) {
        adminService.updateMenuState(id);
        return new ResultVO();
    }

    @PostMapping("/updateNewsState")
    @ResponseBody
    public ResultVO updateNewsState(@RequestParam Integer id) {
        adminService.updateNewsState(id);
        return new ResultVO();
    }

    @PostMapping("/upsertMenu")
    @ResponseBody
    public ResultVO upsertMenu(@RequestBody MenuVO req) {
        var res = adminService.upsertMenu(req);
        return new ResultVO(){{setData(res);}};
    }

    @PostMapping("/upsertNews")
    @ResponseBody
    public ResultVO upsertNews(@RequestBody News req) {
        var res = adminService.upsertNews(req);
        return new ResultVO(){{setData(res);}};
    }

    @PostMapping("/login")
    public String login(@ModelAttribute LoginRequestDTO request, HttpSession httpSession, Model model) {
        try {
            adminService.login(request.getUsername(), request.getPassword());
            httpSession.setAttribute("logined", true);
        } catch (Exception e) {
            model.addAttribute("error", e.getMessage());
            return "admin/login";
        }
        return "redirect:/admin";
    }

    private String checkLogin(HttpSession session, String path) {
        Boolean logined = (Boolean) session.getAttribute("logined");
        if (BooleanUtils.isNotTrue(logined)) {
            return "redirect:/admin/login";
        }
        return path;
    }
}
